<?php

namespace app\admin\model;

use think\Model;

class Admin extends Model
{

	protected $createTime = false;
	protected $updateTime = false;

    public function deladmin($id){
		if ($this::destroy($id)) {
			return 1;
		}else {
			return 2;
		}

	}

    public function saveadmin($data,$admins){
		if (!$data['password']) {
				$data['password'] = $admins['password'];
			}else{
				$data['password'] = md5($data['password']);
			}
			return $this::update(['name'=>$data['name'],'password'=>$data['password'],'id'=>$data['id']]);
	}

    public function addadmin($data){
		if (empty($data) || !is_array($data)) {
			return false;
		}
		if ($data['password']) {
			$data['password'] = md5($data['password']);
		}
		$adminData = array();
		$adminData['name']=$data['name'];
        $adminData['password']=$data['password'];
		if ($this->save($adminData)) {
			// $groupAccess['uid']=$this->id;
			// $groupAccess['group_id']=$data['group_id'];
			// db('auth_group_access')->insert($groupAccess);
			return true;
		}else {
			return false;
		}
	}

    public function login($data){
        $admin = Admin::getByName($data['name']);
        if($admin){
            //能够查询到此用户,并对比数据库
            $userInfo = $this->where('name',$data['name'])->where('password',md5($data['password']))->find();
            if($userInfo){
                session('id',$userInfo['id']);
                session('name',$userInfo['name']);
                return ['valid'=>1,'msg'=>'登录成功'];
            }else{
                return ['valid'=>0,'msg'=>'用户名或者密码不正确'];
            }
        }else{
            //不能够查询到此用户
        	return ['valid'=>0,'msg'=>'该管理员不存在'];
        }
    }

    public function getadmin(){
		return $this::paginate(5);
	}


}
